package cn.lds.j375.chlm.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        Object loginUser = session.getAttribute("loginUser");

        //如果没有登录,跳转到登录页
        if (Objects.isNull(loginUser)){
            request.setAttribute("err","请登录后再来访问");
            request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request,response);
        }


        return true;
    }
}
